<?php

namespace app\controller;

use app\service\BIService;
use app\service\OptionsService;
use app\service\StepCategoryService;
use support\Request;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class BIController
{
    private BIService $bIService;
    private StepCategoryService $stepCategoryService;

    public function __construct()
    {
        $this->bIService = BIService::getInstance();
        $this->stepCategoryService = StepCategoryService::getInstance();
    }

    /**
     * 项目列表
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function projectList(Request $request): Response
    {
        $res = $this->bIService->projectList($request->getTenantId());
        return json(success_response('', $res));
    }

    /**
     * 项目人力资源分布情况
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function projectManResourceDistribution(Request $request): Response
    {
        $projectId = $request->post()['project_id'];
        $res = $this->bIService->projectManResourceDistribution($projectId);
        return json(success_response('', $res));
    }

    /**
     * 项目详情
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function projectDetail(Request $request)
    {
        $projectId = $request->post()['project_id'];
        $res = $this->bIService->projectDetail($projectId);
        return json(success_response('', $res));
    }

    //项目任务完成数量分组统计
    public function projectTaskDoneGroupedCount(Request $request)
    {
        $projectId = $request->post()['project_id'];
        $res = $this->bIService->projectTaskDoneGroupedCount($projectId);
        return json(success_response('', $res));
    }

    /**
     * 项目任务提交趋势
     * @param Request $request
     * @return Response
     */
    public function projectTaskSubmitCountByDate(Request $request): Response
    {
        $projectId = $request->post()['project_id'];
        $dateStringStart = $request->post()['date_string_start'];
        $dateStringEnd = $request->post()['date_string_end'];
        $res = $this->bIService->projectTaskSubmitCountByDate($projectId, $dateStringStart, $dateStringEnd);
        return json(success_response('', $res));
    }

    /**
     * 项目环节产出情况
     * @param Request $request
     * @return Response
     */
    public function projectStepCategoryDoneCount(Request $request): Response
    {
        $projectId = $request->post()['project_id'];
        $res = $this->bIService->projectStepCategoryDoneCount($projectId);
        return json(success_response('', $res));
    }


    /**
     * 项目资产巡检
     * @param Request $request
     * @return Response
     */
    public function projectAssetPreviewCheck(Request $request): Response
    {
        $projectId = $request->post()['project_id'];
        $res = $this->bIService->projectAssetPreviewCheck($projectId);
        return json(success_response('', $res));
    }

    /**
     * 项目进行中剧集
     * @param Request $request
     * @return Response
     */
    public function projectInprogressEpisode(Request $request): Response
    {
        $projectId = $request->post()['project_id'];
        $res = $this->bIService->projectInprogressEpisode($projectId);
        return json(success_response('', $res));
    }

    /**
     * @title 排期分析------需求新增任务统计
     * @param $params
     * @return array|array[]
     */
    public function demandIncTaskAnalysisResult(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "step_category_id" => $request->post()['step_category_id'],
            "start_time" => $request->post()['start_time'],
            "end_time" => $request->post()['end_time'],
            "test" => $request->post()['test']
        ];
        $res = $this->bIService->demandIncTaskAnalysisResult($params);
        return json(success_response('', $res));
    }

    /**
     * @title 排期分析------返修新增任务统计
     * @param $params
     * @return array|array[]
     */
    public function repairIncTaskAnalysisResult(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "step_category_id" => $request->post()['step_category_id'],
            "start_time" => $request->post()['start_time'],
            "end_time" => $request->post()['end_time'],
            "test" => $request->post()['test']
        ];
        $res = $this->bIService->repairIncTaskAnalysisResult($params);
        return json(success_response('', $res));
    }

    /**
     * @title 排期分析------取消废除任务统计
     * @param $params
     * @return array|array[]
     */
    public function cancelTaskAnalysisResult(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "step_category_id" => $request->post()['step_category_id'],
            "start_time" => $request->post()['start_time'],
            "end_time" => $request->post()['end_time'],
            "test" => $request->post()['test']
        ];
        $res = $this->bIService->cancelTaskAnalysisResult($params);
        return json(success_response('', $res));
    }

    /**
     * @title 排期分析------原计划任务统计
     * @param $params
     * @return array|array[]
     */
    public function originTaskAnalysisResult(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "step_category_id" => $request->post()['step_category_id'],
            "start_time" => $request->post()['start_time'],
            "end_time" => $request->post()['end_time'],
            "test" => $request->post()['test']
        ];
        $res = $this->bIService->originTaskAnalysisResult($params);
        return json(success_response('', $res));
    }

    /**
     * @title 排期分析------延期任务统计
     * @param $params
     * @return array|array[]
     */
    public function delayTaskAnalysisResult(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "step_category_id" => $request->post()['step_category_id'],
            "direction" => $request->post()['direction'],
            "start_time" => $request->post()['start_time'],
            "end_time" => $request->post()['end_time'],
            "test" => $request->post()['test']
        ];
        $res = $this->bIService->delayTaskAnalysisResult($params);
        return json(success_response('', $res));
    }

    //按任务原计划完成时间，统计原计划任务数量
    public function projectInTaskDoneTimeCount(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "start_time" => $request->post()['start_time'],
            "end_time" => $request->post()['end_time'],
            "step_category_id" => $request->post()['step_category_id']
        ];
        $res = $this->bIService->projectInTaskDoneTimeCount($params);
        return json(success_response('', $res));
    }

    //统计任务会议+审核次数
    public function projectMeetingReviewCount(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "start_time" => $request->post()['start_time'],
            "end_time" => $request->post()['end_time'],
            "step_category_id" => $request->post()['step_category_id']
        ];
        $res = $this->bIService->projectMeetingReviewCount($params);
        return json(success_response('', $res));
    }

    //按时间统计单集任务进度
    public function projectEpisodeCurrentProgressInTime(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "start_time" => $request->post()['start_time'],
            "end_time" => $request->post()['end_time'],
            "step_category_id" => $request->post()['step_category_id'],
            "page" => $request->post()['page'] ?? 1,
            "pagesize" => $request->post()['pagesize'] ?? 20
        ];
        $res = $this->bIService->projectEpisodeCurrentProgressInTime($params);
        return json(success_response('', $res));
    }

    //按环节统计单集任务进度 单集 单环节
    public function projectEpisodesStepCountResutlList(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "start_time" => $request->post()['start_time'],
            "end_time" => $request->post()['end_time'],
            "step_category_id" => $request->post()['step_category_id'],
            "test" => $request->post()['test']
        ];
        $res = $this->bIService->projectEpisodesStepCountResutlList($params);
        return json(success_response('', $res));
    }

    /**
     * @param Request $request
     * $reqeust{
     * project_id 项目id
     * epicode 集code
     * }
     * @return Response
     */
    public function projectEpisodeCurrentProgressInStep_Item(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "episode" => $request->post()['episode'],
            "start_time" => $request->post()['start_time'],
            "end_time" => $request->post()['end_time'],
            "step_category_id" => $request->post()['step_category_id'],
            "test" => $request->post()['test']
        ];
        $res = $this->bIService->projectEpisodeCurrentProgressInStep_Item($params);
        return json(success_response('', $res));
    }

    //阶段列表
    public function getOpenTemplateTodoStageList(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id']
        ];
        $res = $this->bIService->getOpenTemplateTodoStageList($params);
        return json(success_response('', $res));
    }

    //统计剧集阶段
    public function projectEpisodeStageList(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'],
            "episode_code" => $request->post()['episode_code'],
            "stage_id" => $request->post()['stage_id'],
            "test" => $request->post()['test']
        ];
        $res = $this->bIService->projectEpisodeStageList($params);
        return json(success_response('', $res));
    }

    /**
     * 获取显示工序分类组（项目环节及其工序汇总列表）
     * @return Request {project_id}
     * @throws \think\Exception
     */
    public function getShowStepCategoryGroup(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id']
        ];
        $res = $this->stepCategoryService->getShowStepCategoryGroup($params);
        return json(success_response('', $res));
    }

    /**
     * 查询分类的工序任务模板列表
     */
    public function queryStepCategoryWorkflowTemplatesStepList(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'] ?? null,
            "step_category_id" => $request->post()['step_category_id'] ?? null,// 环节分类id
            "test" => $request->post()['test'] ?? false
        ];
        $res = $this->bIService->queryStepCategoryWorkflowTemplatesStepList($params);
        return json(success_response('', $res));
    }

    /**
     * 查询环节分类列表
     */
    public function queryTopLevelStepCategoryList(Request $request)
    {
        $params = [
            "step_category_id" => $request->post()['step_category_id'] ?? null,// 环节分类id
            "test" => $request->post()['test'] ?? false
        ];
        $res = $this->bIService->queryTopLevelStepCategoryList($params);
        return json(success_response('', $res));
    }

    /**
     * 查询指定分类指定工序模板的所有工序的上游工序列表 一层
     */
    public function queryStepCategoryStepsUpstreamSteps(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'] ?? null,// 项目id
            "step_category_id" => $request->post()['step_category_id'] ?? null,// 环节分类id
            "workflow_id" => $request->post()['workflow_id'] ?? null,// 工序模板id
            "test" => $request->post()['test'] ?? false
        ];
        $res = $this->bIService->queryStepCategoryStepsUpstreamSteps($params);
        return json(success_response('', $res));
    }

    /**
     * 查询指定分类指定工序模板的所有工序的下游工序列表 一层
     */
    public function queryStepCategoryStepsDownstreamSteps(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'] ?? null,// 项目id
            "step_category_id" => $request->post()['step_category_id'] ?? null,// 环节分类id
            "workflow_id" => $request->post()['workflow_id'] ?? null,// 工序模板id
            "test" => $request->post()['test'] ?? false
        ];
        $res = $this->bIService->queryStepCategoryStepsDownstreamSteps($params);
        return json(success_response('', $res));
    }

    /**
     * 查询当前集的所处环节分类下有影响问题的的任务数量
     */
    public function queryCurrentStepCategoryEffectTaskList(Request $request)
    {
        $params = [
            "project_id" => $request->post()['project_id'] ?? null,// 项目id
            "episode_code" => $request->post()['episode_code'] ?? null,// 剧集code
            "step_category_id" => $request->post()['step_category_id'] ?? null,// 环节分类id
            "objectname" => $request->post()['objectname'] ?? 'entity',// 要查询的目标对象名称
            "test" => $request->post()['test'] ?? false
        ];
        $res = $this->bIService->queryCurrentStepCategoryEffectTaskList($params);
        return json(success_response('', $res));
    }
}
